/*
 * jxDao Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxDao.dao.filter;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import org.jxUtils.check.CheckUtil;
import org.jxUtils.check.ParameterCheck;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxDao
 * @package org.jxDao.dao.filter
 * @date 16/12/2014 - 18:27:55
 */
public final class GroupBy implements Serializable {
	
	private static final long serialVersionUID = 5044082921665285884L;
	
	private final StringBuilder strGroupBy = new StringBuilder(FilterOperator.GROUP_BY.toString());
	
	/**
	 * @param columns
	 */
	public GroupBy(final List<String> columns) {
	
		addGroupBy(columns);
	}
	
	/**
	 * @param column
	 */
	public GroupBy(final String column) {
	
		this(new String[]{column});
	}
	
	/**
	 * @param columns
	 */
	public GroupBy(final String[] columns) {
	
		this(Arrays.asList(columns));
	}
	
	/**
	 * @param columns
	 * @return this
	 */
	public GroupBy addGroupBy(final List<String> columns) {
	
		ParameterCheck.isNullOrEmpty(columns, "columns");
		
		//
		for(final String column : columns) {
			
			//
			if(CheckUtil.isNotNullAndNotAbsoluteEmpty(column)) {
				
				//
				if( !strGroupBy.toString().equals(FilterOperator.GROUP_BY.toString())) {
					
					strGroupBy.append(", ");
				}
				
				strGroupBy.append(column);
			}
		}
		
		return this;
	}
	
	/**
	 * @param column
	 * @return this
	 */
	public GroupBy addGroupBy(final String column) {
	
		ParameterCheck.isNullOrAbsoluteEmpty(column, "column");
		
		return addGroupBy(new String[]{column});
	}
	
	/**
	 * @param columns
	 * @return this
	 */
	public GroupBy addGroupBy(final String[] columns) {
	
		ParameterCheck.isNullOrEmpty(columns, "columns");
		
		return addGroupBy(Arrays.asList(columns));
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
	
		return strGroupBy.toString();
	}
}
